/*
 * This is the class for the Chat Messages displayed in the Main Window
 */
package model;

import java.util.ArrayList;
import java.util.Observable;

/**
 * ChatMsgs is in charge of receiving parameters such as sender netname,
 * receivers netname, message, etc and generate a html formatted string with it.
 * It then sends it to the observers in the GUI.
 *
 * @author gb
 */
public class ChatMsgs extends Observable {

    private static String userColor = "#7080e2";
    private static String remoteColor = "#e96565";
    private static String rightArrow = "&#8594;";

    /**
     * Generate an html message from the arguments and updates the GUI with it
     *
     * @param message the message to be shown
     * @param from the netname of the sender
     * @param to the arraylist of destinated users
     */
    public void addSentTextMessage(String message, String from, ArrayList<String> to) {
        String last = messageSentToString(message, from, to);
        this.setChanged();
        this.notifyObservers(last);
    }

    /**
     * Generate an html message from the arguments and updates the GUI with it
     *
     * @param message the message to be shown
     * @param from the netname of the sender
     * @param localnetname the netname of the localuser
     * @param to the arraylist of destinated users
     */
    public void addReceivedTextMessage(String message, String from, String localnetname, ArrayList<String> to) {
        String last = messageReceivedToString(message, from, localnetname, to);
        this.setChanged();
        this.notifyObservers(last);
    }

    /**
     * Generate an html message according to the boolean, the message will say
     * whether or not the file has been received.
     *
     * @param fileName the name of the file
     * @param from the netname of the sender
     * @param localnetname the netname of the localuser
     * @param to the arraylist of destinated users
     * @param boolReceived true if file received, false if not
     */
    public void addTextNotifyFileReception(String fileName, String from, String localnetname, ArrayList<String> to, boolean boolReceived) {
        String message = "";
        if (boolReceived) {
            message = "File " + fileName + " received";
        } else {
            message = "File " + fileName + " not received";
        }
        String last = messageReceivedToString(message, from, localnetname, to);

        this.setChanged();
        this.notifyObservers(last);
    }

    /**
     * Generate an html message according to the boolean, the message will say
     * whether or not the file has been transmitted.
     *
     * @param fileName the name of the file
     * @param from the netname of the sender which is the local user net name
     * @param remoteUsername the net name of the receiver
     * @param boolTransmitted true if file transmitted, false if not.
     */
    public void addTextNotifyFileTransmition(String fileName, String from, String remoteUsername, boolean boolTransmitted) {
        String message = "";
        if (boolTransmitted) {
            message = "File " + fileName + " transmitted";
        } else {
            message = "File " + fileName + " not transmitted";
        }
        ArrayList<String> to = new ArrayList<String>();
        to.add(remoteUsername);
        String last = messageSentToString(message, from, to);
        this.setChanged();
        this.notifyObservers(last);
    }

    /**
     * Creates the html message from the parameters.
     * <br/> ex : from->to/localnetname,localnetname : <br/>
     * message
     *
     * from is a link to the sender of the message.<br/> to/localnetname is a
     * link to all receivers but us and also the sender.<br/> the message is
     * printed in remoteColor.</br>
     *
     * @param message the message that has been received
     * @param from the sender net name
     * @param localnetname the local netname
     * @param to the arraylist of receivers
     * @return the html formatted message as a String
     */
    private String messageReceivedToString(String message, String from, String localnetname, ArrayList<String> to) {
        String last = "";
        //If we are not the only one to whom the message was sent
        if (to.size() > 1) {
            String sto = AL2StringWithoutUs(to, localnetname);
            last = "<a href=\"" + from + "\">" + from + "</a>" + rightArrow + "<a href=\"" + sto + from + "\">" + sto + "</a>" + localnetname;
            last += ": <br/><font color=\"" + remoteColor + "\">" + message + "</font><br/>";
        } else {
            last = "<a href=\"" + from + "\">" + from + "</a>" + rightArrow + localnetname;
            last += ": <br/><font color=\"" + remoteColor + "\">" + message + "</font><br/>";
        }
        return last;
    }

    /**
     * Creates the html message from the parameters.
     * <br/> ex : localnetname->to : <br/>
     * message
     *
     * localnetname is not a link.<br/> to is a link to all receivers but us and
     * also the sender.<br/> the message is printed in remoteColor.</br>
     *
     * @param message the message that has been received
     * @param localnetname the local netname
     * @param to the arraylist of receivers
     * @return the html formatted message as a String
     */
    private String messageSentToString(String message, String localnetname, ArrayList<String> to) {
        String last = "";
        String sto = AL2String(to);
        last = localnetname + rightArrow + "<a href=\"" + sto + "\">" + sto + "</a>";
        last += ": <br/><font color=\"" + userColor + "\">" + message + "</font><br/>";
        return last;
    }

    /**
     * Takes an arraylist and returns a string
     */
    private String AL2String(ArrayList<String> to) {
        String res = "";
        for (String s : to) {
            res += s + ", ";
        }
        return res;
    }

    /**
     * Takes an arraylist and returns a string without the string from
     */
    private String AL2StringWithoutUs(ArrayList<String> to, String from) {
        String res = "";
        for (String s : to) {
            if (!s.equals(from)) {
                res += s + ", ";
            }
        }
        return res;
    }
}
